<?php

	class GenerateImageThumbnailCommand implements ICommand
	{
		public function execute($event)
		{
			$md_id	 	= $event->data['media'];
			$folder_id 	= $event->data['folder'];
			
			$mediaitemmodel = EbModel::getModel('Media','MediaItem');
			$main_files 	= $mediaitemmodel->selectByMedia($md_id,'AND type=1');
			$folder_path	= EbModel::getModel('Media','MediaFolder')->getFolderPath($folder_id);
			$provider		= new Media_HttpProvider(ROOT,'/',$folder_path);
			
			if (count($main_files) == 1)
			{
				$main_file = $main_files[0];
								
				$extension = EbModel::getModel('Media','MediaExtension')->selectById_1($main_file->ex_id);
				
				if (!is_null($extension))
				{				
					$image_helper 	= new Media_ImageHelper(100,100);
					$source 		= $provider->getFilePath($main_file->code);
					$code 			= $provider->getCode($extension->name);
					$target			= $provider->getFilePath($code);

					$image_helper->createthumb($source,$target);
										
					$thumbnailfile = (object) array('name' => $main_file->name.'_Thumbnail','pr_id' => 1,'type' => 2, 'md_id' => $md_id,'ex_id' => $extension->ex_id,'code' => $code);
						
					$mediaitemmodel->insert($thumbnailfile);
				}
				else
				{
					die('This extension is unknown!');
				}
			}
			else
			{
				die('This file is unknown!');
			}
		}
	}